package com.example.controller;

import com.example.pojo.*;
import com.example.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

//声明这个类负责接收前端请求并返回响应
@RestController
//规定控制台输出的格式
@Slf4j
//定义一个公开访问的类，即controller，类名命名规则：页面名+Controller
public class goodProductConditionsController {

    //自动依赖注入
    //自动将 measuringToolsListService 的实例注入到当前类，并会给service中自动生成一段代码
    @Autowired
    private measuringToolsListService measuringToolsListService;
    @Autowired
    private measuringToolChecklistService measuringToolChecklistService;
    @Autowired
    private calibrationRecordService calibrationRecordService;
    @Autowired
    private goodproductQuestionlistService goodproductQuestionlistService;
    @Autowired
    private fixtureTechConditionService fixtureTechConditionService;
    @Autowired
    private fixtureListService fixtureListService;
    @Autowired
    private fixtureChecklistService fixtureChecklistService;
    @Autowired
    private oilListService oilListService;
    @Autowired
    private oilChecklistService oilChecklistService;
    @Autowired
    private minorDefectsRecordService minorDefectsRecordService;
    @Autowired
    private urgentTeardownStandardService urgentTeardownStandardService;
    @Autowired
    private urgentTeardownResultService urgentTeardownResultService;
    @Value("${file.upload-dir}")
    private String saveDir;

    //————————————————————————————————————————————————————————————————————————————————————
    //量检具基本信息
    //声明该方法处理 POST 请求，路径为 /insertMeasuringToolsListGoodProduct
    //向计量器具基本信息表中插入一行数据
    @PostMapping("/insertMeasuringToolsListGoodProduct")
    //接收返回值
    public Result insertMeasuringToolsListGoodProduct(@RequestBody measuringToolsListPojo pojo){
        //第一步：向基本信息表中插入一行
        //将接收到的pojo对象传递给 measuringToolsListService.insertOne() 方法，执行插入逻辑
        measuringToolsListService.insertOne(pojo);
        //第二步：判断是否需要校准
        if (Objects.equals(pojo.getIs_calibration(),"yes")){
            calibrationRecordPojo calibrationRecordPojo = new calibrationRecordPojo();
            calibrationRecordPojo.setNumber(pojo.getNumber());
            calibrationRecordPojo.setName(pojo.getName());
            calibrationRecordPojo.setName_english(pojo.getName_english());
            calibrationRecordPojo.setName_russian(pojo.getName_russian());
            calibrationRecordPojo.setSpecification(pojo.getSpecification());
            calibrationRecordPojo.setStandard_value(pojo.getStandard_value());
            calibrationRecordPojo.setUpper_tolerance(pojo.getUpper_tolerance());
            calibrationRecordPojo.setLower_tolerance(pojo.getLower_tolerance());
            calibrationRecordPojo.setCalibration_cycle(pojo.getCalibration_cycle());
            calibrationRecordPojo.setGroup(pojo.getGroup());
            calibrationRecordPojo.setSys_deleted(0);
            calibrationRecordService.insertOneRow(calibrationRecordPojo);
        }
        //返回一个成功的 Result 对象，消息为“插入成功”
        return Result. success("插入成功");
    }

    //查询计量器具基本信息表中所有的数据
    @PostMapping("/queryMeasuringToolsListAllGoodProduct")
    public Result queryMeasuringToolsListAllGoodProduct(){
        //调用measuringToolsListService.queryAll()方法获取数据
        //返回结果存储在List<measuringToolsListPojo>类型的pojoList变量中
        List<measuringToolsListPojo> pojoList = measuringToolsListService.queryAll();
        //返回一个成功的 Result 对象，对象为表单中所有数据
        return Result.success(pojoList);
    }

    //根据Id编辑某一行数据，基本信息表
    //处理前端post请求，接通与前端的接口路径，（最好从前端复制过来，避免写错）
    @PostMapping("/editMeasuringToolsListGoodProduct")
    public Result editMeasuringToolsListGoodProduct(@RequestBody measuringToolsListPojo pojo){
        //需要同时修改量检具的点检表、校准表

        //第一步，需要先把原始数据拿出来，去基础信息表中找到原始数据
        measuringToolsListPojo oldData=measuringToolsListService.selectById(pojo.getId());

        //第二步：修改基本信息表
        //将接收到的pojo对象传递给 measuringToolsListService..editOneByIdUse() 方法，执行编辑某一行逻辑
        measuringToolsListService.editOneByIdUse(pojo);
        //第三步：修改点检表：
        String fieldName;
        String old;
        String newData;
        if (!Objects.equals(oldData.getNumber(), pojo.getNumber())){
            fieldName="number";
            old=oldData.getNumber();
            newData=pojo.getNumber();
            measuringToolChecklistService.editByField(fieldName,old,newData);
            calibrationRecordService.editByField(fieldName,old,newData);
        }
        if (!Objects.equals(oldData.getName(), pojo.getName())){
            fieldName="name";
            old=oldData.getName();
            newData=pojo.getName();
            measuringToolChecklistService.editByField(fieldName,old,newData);
            calibrationRecordService.editByField(fieldName,old,newData);
        }
        if (!Objects.equals(oldData.getName_english(), pojo.getName_english())){
            fieldName="name_english";
            old=oldData.getName_english();
            newData=pojo.getName_english();
            measuringToolChecklistService.editByField(fieldName,old,newData);
            calibrationRecordService.editByField(fieldName,old,newData);
        }
        if (!Objects.equals(oldData.getName_russian(), pojo.getName_russian())){
            fieldName="name_russian";
            old=oldData.getName_russian();
            newData=pojo.getName_russian();
            measuringToolChecklistService.editByField(fieldName,old,newData);
            calibrationRecordService.editByField(fieldName,old,newData);
        }

        //第四步：修改校准表：
        if (!Objects.equals(oldData.getSpecification(), pojo.getSpecification())){
            fieldName="specification";
            old=oldData.getSpecification();
            newData=pojo.getSpecification();
            calibrationRecordService.editByField(fieldName,old,newData);
        }
        if (!Objects.equals(oldData.getStandard_value(), pojo.getStandard_value())){
            fieldName="standard_value";
            old=oldData.getStandard_value();
            newData=pojo.getStandard_value();
            calibrationRecordService.editByField(fieldName,old,newData);
        }
        if (!Objects.equals(oldData.getUpper_tolerance(), pojo.getUpper_tolerance())){
            fieldName="upper_tolerance";
            old=oldData.getUpper_tolerance();
            newData=pojo.getUpper_tolerance();
            calibrationRecordService.editByField(fieldName,old,newData);
        }
        if (!Objects.equals(oldData.getLower_tolerance(), pojo.getLower_tolerance())){
            fieldName="lower_tolerance";
            old=oldData.getLower_tolerance();
            newData=pojo.getLower_tolerance();
            calibrationRecordService.editByField(fieldName,old,newData);
        }
        if (!Objects.equals(oldData.getCalibration_cycle(), pojo.getCalibration_cycle())){
            fieldName="calibration_cycle";
            old=oldData.getCalibration_cycle();
            newData=pojo.getCalibration_cycle();
            calibrationRecordService.editByField(fieldName,old,newData);
        }
        if (!Objects.equals(oldData.getGroup(), pojo.getGroup())){
            fieldName="group";
            old=oldData.getGroup();
            newData=pojo.getGroup();
            calibrationRecordService.editByField(fieldName,old,newData);
        }
        if (!Objects.equals(oldData.getSys_deleted(), pojo.getSys_deleted())){
            fieldName="sys_deleted";
            String fieldVal=pojo.getNumber();
            int deleteVal=pojo.getSys_deleted();
            //根据编号，修改删除标记位
            calibrationRecordService.editByFieldInt(fieldName,fieldVal,deleteVal);
        }
        //返回一个成功的 Result 对象，消息为“修改成功”
        return Result.success("修改成功");
    }

    //————————————————————————————————————————————————————————————————————————————————————
    //量检具点检管理
    //接收创建时间字段的数据，定一个接收范围
    //处理前端post请求，接通与前端的接口路径，（最好从前端复制过来，避免写错）
    @PostMapping("/queryMeasuringToolChecklistOneFieldScopeGoodProduct")
    public Result queryMeasuringToolChecklistOneFieldScopeGoodProduct(@RequestBody Map<String,Object> map){
        List<measuringToolChecklistPojo> pojoList= measuringToolChecklistService.queryOneField(map);
        return Result.success(pojoList);
    }

    //删除点检表中的某一行数据
    @PostMapping("/editMeasuringToolsChecklistGoodProduct")
    public Result editMeasuringToolsChecklistGoodProduct(@RequestBody measuringToolChecklistPojo pojo){
        measuringToolChecklistService.editOneById(pojo);
        return Result.success("删除成功");
    }

    //插入一条点检记录
    @PostMapping("/insertMeasuringToolChecklistGoodProduct")
    public Result insertMeasuringToolChecklistGoodProduct(@RequestBody measuringToolChecklistPojo pojo){
        //第一步：向点检记录中插入一行
        //将接收到的pojo对象传递给 measuringToolChecklistService.insertOne() 方法，执行插入逻辑
        measuringToolChecklistService.insertOne(pojo);
        //返回一个成功的 Result 对象，消息为“插入成功”
        return Result. success("插入成功");
    }

    //查询点检记录表中编号及产生时间为目标值的数据
    @PostMapping("/queryMeasuringToolChecklistTimeNumberGoodProduct")
    public Result queryMeasuringToolChecklistTimeNumberGoodProduct(@RequestBody Map<String,Object> map){
        List<measuringToolChecklistPojo> pojoList= measuringToolChecklistService.queryTimeNumber(map);
        return Result.success(pojoList);
    }

    //————————————————————————————————————————————————————————————————————————————————————
    //量检具校准记录
    //查询所有量检具校准记录
    @PostMapping("/queryCalibrationRecordGoodProduct")
    public Result queryCalibrationRecordGoodProduct(){
        List<calibrationRecordPojo> pojos=calibrationRecordService.queryAll();
        return Result.success(pojos);
    }

    //修改校准记录表中的某一行数据
    @PostMapping("/editCalibrationRecordGoodProduct")
    public Result editCalibrationRecordGoodProduct(@RequestBody calibrationRecordPojo pojo){
        calibrationRecordService.editOneById(pojo);
        return Result.success("编辑成功");
    }

    //————————————————————————————————————————————————————————————————————————————————————
    //问题清单
    //查询所有问题清单记录
    @PostMapping("/queryQuestionListGoodProduct")
    public Result queryQuestionListGoodProduct(){
        List<goodproductQuestionlistPojo> pojos=goodproductQuestionlistService.queryAll();
        return Result.success(pojos);
    }

    //根据Id编辑某一行数据，问题清单
    //处理前端post请求，接通与前端的接口路径，（最好从前端复制过来，避免写错）
    @PostMapping("/editQuestionListGoodProduct")
    public Result editQuestionListGoodProduct(@RequestBody goodproductQuestionlistPojo pojo){
        //第一步，需要先把原始数据拿出来，去问题清单中找到原始数据
        goodproductQuestionlistPojo oldData=goodproductQuestionlistService.selectById(pojo.getId());

        //第二步：修改问题清单
        //将接收到的pojo对象传递给 goodproductQuestionlistService.editOneById() 方法，执行编辑某一行逻辑
        goodproductQuestionlistService.editOneById(pojo);
        //返回一个成功的 Result 对象，消息为“修改成功”
        return Result.success("修改成功");
    }

    //————————————————————————————————————————————————————————————————————————————————————
    //工装技术条件
    //声明该方法处理 POST 请求，路径为 /insertFixtureTechConditionGoodProduct
    //向工装技术条件中插入一行数据
    @PostMapping("/insertFixtureTechConditionGoodProduct")
    //接收返回值
    public Result insertFixtureTechConditionGoodProduct(@RequestBody fixtureTechConditionPojo pojo){
        //第一步：向工装技术条件中插入一行
        //将接收到的pojo对象传递给 fixtureTechConditionService.insertOne() 方法，执行插入逻辑
        fixtureTechConditionService.insertOne(pojo);
        //返回一个成功的 Result 对象，消息为“插入成功”
        return Result. success("插入成功");
    }

    //根据分页查询工装技术条件
    @PostMapping("/queryFixtureTechnicalConditionPaginationGoodProduct")
    public Result queryFixtureTechnicalConditionPaginationGoodProduct(@RequestBody Map<String,Object> map){
        List<fixtureTechConditionPojo> fixtureTechConditionPojos=fixtureTechConditionService.queryByPage(map);
        return Result.success(fixtureTechConditionPojos);
    }

    //查询所有工装技术条件条数
    @PostMapping("/queryFixtureTechnicalConditionQuantityGoodProduct")
    public Result queryFixtureTechnicalConditionQuantityGoodProduct(@RequestBody Map<String,Object> map){
        Integer quantity= fixtureTechConditionService.queryQuantity(map);
        return Result.success(quantity);
    }

    //查询所有工装技术条件中不重复的图号
    @PostMapping("/queryFixtureDrawingNumberGoodProduct")
    public Result queryFixtureDrawingNumberGoodProduct(){
        List<fixtureTechConditionPojo> pojo= fixtureTechConditionService.queryDrawingNumber();
        return Result.success(pojo);
    }

    //根据Id编辑某一行数据，工装技术条件
    //处理前端post请求，接通与前端的接口路径，（最好从前端复制过来，避免写错）
    @PostMapping("/editFixtureTechConditionGoodProduct")
    public Result editFixtureTechConditionGoodProduct(@RequestBody fixtureTechConditionPojo pojo){
        //第一步，需要先把原始数据拿出来，去基础信息表中找到原始数据
        fixtureTechConditionPojo oldData=fixtureTechConditionService.selectById(pojo.getId());

        //第二步：修改工装技术条件
        //将接收到的pojo对象传递给 fixtureTechConditionService..editOneById() 方法，执行编辑某一行逻辑
        fixtureTechConditionService.editOneById(pojo);

        //第三步：修改工装台账：
        Map<String, String> fieldMapping = new HashMap<>();
        fieldMapping.put("drawing_number", "drawing_number"); // 技术条件字段 -> 台账字段
        fieldMapping.put("engine_model", "model");
        fieldMapping.put("fixture_name", "name");
        fieldMapping.put("fixture_name_english", "name_english");
        fieldMapping.put("fixture_name_russian", "name_russian");

        // 检查并更新每个字段
        updateFixtureListIfChanged("drawing_number", oldData.getDrawing_number(), pojo.getDrawing_number(), fieldMapping);
        updateFixtureListIfChanged("engine_model", oldData.getEngine_model(), pojo.getEngine_model(), fieldMapping);
        updateFixtureListIfChanged("fixture_name", oldData.getFixture_name(), pojo.getFixture_name(), fieldMapping);
        updateFixtureListIfChanged("fixture_name_english", oldData.getFixture_name_english(), pojo.getFixture_name_english(), fieldMapping);
        updateFixtureListIfChanged("fixture_name_russian", oldData.getFixture_name_russian(), pojo.getFixture_name_russian(), fieldMapping);

        //返回一个成功的 Result 对象，消息为“修改成功”
        return Result.success("修改成功");
    }
    // 辅助方法：检查字段是否变化并更新台账
    private void updateFixtureListIfChanged(String techField, String oldValue, String newValue, Map<String, String> fieldMapping) {
        if (!Objects.equals(oldValue, newValue)) {
            String ledgerField = fieldMapping.get(techField); // 获取对应的台账字段名
            if (ledgerField != null) {
                fixtureListService.editByField(ledgerField, oldValue, newValue);
            }
        }
    }

    //查询某一字段为某值的数据
    @PostMapping("/queryFixtureTechConditionOneFieldGoodProduct")
    public Result queryFixtureTechConditionOneFieldGoodProduct(@RequestBody Map<String,Object> map){
        List<fixtureTechConditionPojo> fixtureTechConditionPojos=fixtureTechConditionService.queryOneField(map);
        return Result.success(fixtureTechConditionPojos);
    }

    //查询两个字段为某值的数据
    @PostMapping("/queryFixtureTechConditionTwoFieldsGoodProduct")
    public Result queryFixtureTechConditionTwoFieldsGoodProduct(@RequestBody Map<String,Object> map){
        List <fixtureTechConditionPojo> fixtureTechConditionPojos=fixtureTechConditionService.queryTwoFields(map);
        return Result.success(fixtureTechConditionPojos);
    }

    //—————————————————————————————————————————工装台账———————————————————————————————————————————
    //工装台账
    //声明该方法处理 POST 请求，路径为 /insertFixtureListGoodProduct
    //向工装台账中插入一行数据
    @PostMapping("/insertFixtureListGoodProduct")
    //接收返回值
    public Result insertFixtureListGoodProduct(@RequestBody fixtureListPojo pojo){
        //第一步：向工装台账中插入一行
        //将接收到的pojo对象传递给 fixtureListService.insertOne() 方法，执行插入逻辑
        fixtureListService.insertOne(pojo);

        //返回一个成功的 Result 对象，消息为“插入成功”
        return Result. success("插入成功");
    }

    //查询工装技术条件中所有的数据
    @PostMapping("/queryFixtureListAllGoodProduct")
    public Result queryFixtureListAllGoodProduct(){
        //调用fixtureListService.queryAll()方法获取数据
        //返回结果存储在List<fixtureListPojo>类型的pojoList变量中
        List<fixtureListPojo> pojoList = fixtureListService.queryAll();
        //返回一个成功的 Result 对象，对象为表单中所有数据
        return Result.success(pojoList);
    }

    //根据Id编辑某一行数据，工装技术条件
    //处理前端post请求，接通与前端的接口路径，（最好从前端复制过来，避免写错）
    @PostMapping("/editFixtureListGoodProduct")
    public Result editFixtureListGoodProduct(@RequestBody fixtureListPojo pojo){
        //第一步，需要先把原始数据拿出来，去基础信息表中找到原始数据
        fixtureListPojo oldData=fixtureListService.selectById(pojo.getId());

        //第二步：修改基本信息表
        //将接收到的pojo对象传递给 fixtureListService..editOneById() 方法，执行编辑某一行逻辑
        fixtureListService.editOneById(pojo);
        //第三步：判断工装图纸编号是否更改
        if (oldData.getDrawing_number() == null || pojo.getDrawing_number() == null
                || !oldData.getDrawing_number().equals(pojo.getDrawing_number())){
            //将工装技术条件中该图纸编号关联的若干行数据删除
            String fieldName="fixture_list_id";
            String fieldVal=pojo.getId();
            fixtureTechConditionService.deleteOneField(fieldName,fieldVal);
            //按照新的工装图纸编号查询对应数据，并插入新的若干行数据
            //创建 Map<String, Object>
            Map<String, Object> map = new HashMap<>();
            // 添加键值对
            map.put("fieldName", "drawing_number");
            map.put("fieldVal", pojo.getDrawing_number());
            //调用根据某一字段查询数据的方法(pojoList里面是工装图纸编号为目标值的所有行工装技术条件数据)
            List<fixtureTechConditionPojo> pojoList = fixtureTechConditionService.queryOneField(map);
            //和工装台账进行关联
            for (int i = 0; i < pojoList.size(); i++) {
                pojoList.get(i).setFixture_list_id(pojo.getId());
                fixtureTechConditionService.insertOne(pojoList.get(i));
            }
        }
        //返回一个成功的 Result 对象，消息为“修改成功”
        return Result.success("修改成功");
    }

    //查询某一字段为某值的数据
    @PostMapping("/queryFixtureListOneFieldGoodProduct")
    public Result queryFixtureListOneFieldGoodProduct(@RequestBody Map<String,Object> map){
        List<fixtureListPojo> fixtureListPojos=fixtureListService.queryOneField(map);
        return Result.success(fixtureListPojos);
    }

    //———————————————————————————————————————————工装点检—————————————————————————————————————————
    //工装点检
    @PostMapping("/insertFixtureChecklistGoodProduct")
    public Result insertFixtureChecklistGoodProduct(@RequestBody List<fixtureChecklistPojo> pojoList){
        fixtureChecklistService.insertSeveralRows(pojoList);
        return Result.success("插入成功");
    }

    //——————————————————————————————————————————油液基本信息——————————————————————————————————————————
    //插入一行油液基本信息
    @PostMapping("/insertOilListGoodProduct")
    public Result insertOilListGoodProduct(@RequestBody oilListPojo pojo){
        oilListService.insertOne(pojo);
        return Result.success("插入成功");
    }
    //修改一行油液基本信息
    @PostMapping("/editOilListGoodProduct")
    public Result editOilListGoodProduct(@RequestBody oilListPojo pojo) {
        oilListService.editOneById(pojo);
        return Result.success("修改成功");
    }
    //查询所有的油液基本信息
    @PostMapping("/queryOilListAllGoodProduct")
    public Result queryOilListAllGoodProduct(){
        List<oilListPojo> pojoList=oilListService.queryAll();
        return Result.success(pojoList);
    }
    //查询三个字段为某值的数据
    @PostMapping("/queryOilListThreeFieldsGoodProduct")
    public Result queryOilListThreeFieldsGoodProduct(@RequestBody Map<String,Object> map){
        List <oilListPojo> oilListPojos=oilListService.queryThreeFields(map);
        return Result.success(oilListPojos);
    }

    //——————————————————————————————————————————油液点检记录——————————————————————————————————————————
    //一次插入多行油液点检
    @PostMapping("/insertOilChecklistGoodProduct")
    public Result insertOilChecklistGoodProduct(@RequestBody List<oilChecklistPojo> pojoList){
        oilChecklistService.insertSeveralRows(pojoList);
        return Result.success("插入成功");
    }

    //根据分页查询油液历史点检数据
    @PostMapping("/queryOilHistoricalDataPaginationGoodProduct")
    public Result queryOilHistoricalDataPaginationGoodProduct(@RequestBody Map<String,Object> map){
        List<oilChecklistPojo> oilChecklistPojos=oilChecklistService.queryByPage(map);
        return Result.success(oilChecklistPojos);
    }

    //查询所有油液历史点检数据条数
    @PostMapping("/queryOilHistoricalDataQuantityGoodProduct")
    public Result queryOilHistoricalDataQuantityGoodProduct(@RequestBody Map<String,Object> map){
        Integer quantity= oilChecklistService.queryQuantity(map);
        return Result.success(quantity);
    }

    //修改一行油液点检信息
    @PostMapping("/editOilChecklistGoodProduct")
    public Result editOilChecklistGoodProduct(@RequestBody oilChecklistPojo pojo) {
        oilChecklistService.editOneById(pojo);
        return Result.success("修改成功");
    }

    //动态查询油液历史点检数据（分页）
    @PostMapping("/queryOilHistoricalDataByDynamicConditionsProblemManagementNew")
    public Result queryOilHistoricalDataByDynamicConditionsProblemManagementNew(@RequestBody Map<String,Object> map){
        // 查询分页数据
        List<oilChecklistPojo> data = oilChecklistService.queryByDynamicConditionsWithPagination(map);
        // 查询符合条件的数据总数
        Integer total = oilChecklistService.queryByDynamicConditionsCount(map);

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("data", data);
        result.put("total", total);
        return Result.success(result);
    }

    //查询所有油液历史点检数据
    @PostMapping("/queryOilHistoricalDataAllGoodProduct")
    public Result queryOilHistoricalDataAllGoodProduct(){
        List<oilChecklistPojo> pojoList=oilChecklistService.queryAll();
        return Result.success(pojoList);
    }

    //——————————————————————————————————————————油液趋势管理——————————————————————————————————————————
    //根据点检项目查询指定数量的数据
    @PostMapping("/queryOilProjectByOneFieldGoodProduct")
    public Result queryOilProjectByOneFieldGoodProduct(@RequestBody Map<String,Object> map){
        List<oilChecklistPojo>pojoList = oilChecklistService.queryByOneField(map);
        return Result.success(pojoList);
    }

    //——————————————————————————————————————————微小缺陷——————————————————————————————————————————
    //插入一行微小缺陷
    @PostMapping("/insertMinorDefectRecordGoodProduct")
    public Result insertMinorDefectRecordGoodProduct(@RequestBody minorDefectsRecordPojo pojo){
        minorDefectsRecordService.insertOne(pojo);
        return Result.success("插入成功");
    }
    //修改一行微小缺陷
    @PostMapping("/editMinorDefectRecordGoodProduct")
    public Result editMinorDefectRecordGoodProduct(@RequestBody minorDefectsRecordPojo pojo) {
        minorDefectsRecordService.editOneById(pojo);
        return Result.success("修改成功");
    }

    //根据分页查询微小缺陷数据
    @PostMapping("/queryMinorDefectsRecordPaginationGoodProduct")
    public Result queryMinorDefectsRecordPaginationGoodProduct(@RequestBody Map<String,Object> map){
        List<minorDefectsRecordPojo> minorDefectsRecordPojos=minorDefectsRecordService.queryByPage(map);
        return Result.success(minorDefectsRecordPojos);
    }

    //查询所有微小缺陷条数
    @PostMapping("/queryMinorDefectsRecordQuantityGoodProduct")
    public Result queryMinorDefectsRecordQuantityGoodProduct(@RequestBody Map<String,Object> map){
        Integer quantity= minorDefectsRecordService.queryQuantity(map);
        return Result.success(quantity);
    }

    //——————————————————————————————————————————特急分解检测标准——————————————————————————————————————————
    //插入一行特急分解检测标准
    @PostMapping("/insertUrgentTeardownStandardGoodProduct")
    public Result insertUrgentTeardownStandardGoodProduct(@RequestBody urgentTeardownStandardPojo pojo){
        urgentTeardownStandardService.insertOne(pojo);
        return Result.success("插入成功");
    }
    //修改一行特急分解检测标准
    @PostMapping("/editUrgentTeardownStandardGoodProduct")
    public Result editUrgentTeardownStandardGoodProduct(@RequestBody urgentTeardownStandardPojo pojo) {
        urgentTeardownStandardService.editOneById(pojo);
        return Result.success("修改成功");
    }

    //查询所有特急分解检测标准
    @PostMapping("/queryUrgentTeardownStandardAllGoodProduct")
    public Result queryUrgentTeardownStandardAllGoodProduct(){
        List<urgentTeardownStandardPojo> pojoList=urgentTeardownStandardService.queryAll();
        return Result.success(pojoList);
    }

    //根据分页查询特急分解检测标准
    @PostMapping("/queryUrgentTeardownStandardPaginationGoodProduct")
    public Result queryUrgentTeardownStandardPaginationGoodProduct(@RequestBody Map<String,Object> map){
        List<urgentTeardownStandardPojo> urgentTeardownStandardPojos=urgentTeardownStandardService.queryByPage(map);
        return Result.success(urgentTeardownStandardPojos);
    }
    //查询所有特急分解检测标准总条数
    @PostMapping("/queryUrgentTeardownStandardQuantityGoodProduct")
    public Result queryUrgentTeardownStandardQuantityGoodProduct(){
        Integer quantity= urgentTeardownStandardService.queryQuantity();
        return Result.success(quantity);
    }

    //——————————————————————————————————————————特急分解检测结果——————————————————————————————————————————
    //查询特急分解检测标准一个字段为某值的数据
    @PostMapping("/queryUrgentTeardownStandardOneFieldGoodProduct")
    public Result queryUrgentTeardownStandardOneFieldGoodProduct(@RequestBody Map<String,Object> map){
        List <urgentTeardownStandardPojo> pojo=urgentTeardownStandardService.queryOneField(map);
        return Result.success(pojo);
    }
    //查询特急分解检测结果一个字段为某值的数据
    @PostMapping("/queryUrgentTeardownResultOneFieldGoodProduct")
    public Result queryUrgentTeardownResultOneFieldGoodProduct(@RequestBody Map<String,Object> map){
        List <urgentTeardownResultPojo> pojo=urgentTeardownResultService.queryOneField(map);
        return Result.success(pojo);
    }

    //向特急分解检测结果中插入多行
    @PostMapping("/insertSeveralRowsUrgentTeardownGoodProduct")
    public Result insertSeveralRowsUrgentTeardownGoodProduct(@RequestBody List<urgentTeardownResultPojo> pojos){
        urgentTeardownResultService.insertSeveralRows(pojos);
        return Result.success("保存成功");
    }

    //查询所有特急分解检测结果
    @PostMapping("/queryUrgentTeardownResultAllGoodProduct")
    public Result queryUrgentTeardownResultAllGoodProduct(){
        List<urgentTeardownResultPojo> pojoList=urgentTeardownResultService.queryAll();
        return Result.success(pojoList);
    }

    //根据分页查询特急分解检测结果
    @PostMapping("/queryUrgentTeardownResultPaginationGoodProduct")
    public Result queryUrgentTeardownResultPaginationGoodProduct(@RequestBody Map<String,Object> map){
        List<urgentTeardownResultPojo> urgentTeardownResultPojos=urgentTeardownResultService.queryByPage(map);
        return Result.success(urgentTeardownResultPojos);
    }
    //查询所有特急分解检测结果总条数
    @PostMapping("/queryUrgentTeardownResultQuantityGoodProduct")
    public Result queryUrgentTeardownResultQuantityGoodProduct(){
        Integer quantity= urgentTeardownResultService.queryQuantity();
        return Result.success(quantity);
    }

    //修改一行特急分解检测结果
    @PostMapping("/editUrgentTeardownResultGoodProduct")
    public Result editUrgentTeardownResultGoodProduct(@RequestBody urgentTeardownResultPojo pojo) {
        urgentTeardownResultService.editOneById(pojo);
        return Result.success("修改成功");
    }

    //查询特急分解检测结果五个字段为某值的数据
    @PostMapping("/queryUrgentTeardownResultFiveFieldsGoodProduct")
    public Result queryUrgentTeardownResultFiveFieldsGoodProduct(@RequestBody Map<String,Object> map){
        List<urgentTeardownResultPojo> pojos=new ArrayList<>();
        if (map.size()==2){
            pojos=urgentTeardownResultService.queryOneField(map);
        }
        else if (map.size()==4){
            pojos=urgentTeardownResultService.queryTwoFields(map);
        }
        else if (map.size()==6){
            pojos=urgentTeardownResultService.queryThreeFields(map);
        }
        else if (map.size()==8){
            pojos=urgentTeardownResultService.queryFourFields(map);
        }
        else if (map.size()==10){
            pojos=urgentTeardownResultService.queryFiveFields(map);
        }
        return Result.success(pojos);
    }

    //上传特急分解检测结果照片
    @PostMapping("/editUrgentTeardownResultPictureGoodProduct")
    public Result editUrgentTeardownResultPictureGoodProduct(
            @RequestParam("file") MultipartFile file,
            @RequestParam String id,
            @RequestParam String fileName){
        try {
            if (!file.isEmpty()) {
                String filePath = saveDir + fileName;
                File dest = new File(filePath);
                // 将文件保存到服务器上的指定位置
                file.transferTo(dest);
                System.out.println("保存成功");
                String fieldName="file_name";
                String fieldValue=fileName;
                urgentTeardownResultService.editOneField(id, fieldName, fieldValue);
            }
            return Result.success("保存成功");
        }
        catch (IOException e) {
            // 捕获异常并返回错误消息
            throw new RuntimeException("文件保存失败：" + e.getMessage());
        }
    }

    //修改多行特急分解检测结果(录入检测结果)
    @PostMapping("/uploadUrgentTeardownResultGoodProduct")
    public Result uploadUrgentTeardownResultGoodProduct(@RequestBody List<urgentTeardownResultPojo> pojos) {
        urgentTeardownResultService.uploadById(pojos);
        return Result.success("修改成功");
    }

    //动态查询特急分解检测结果
    @PostMapping("/queryUrgentTeardownResultByDynamicConditionsGoodProduct")
    public Result queryUrgentTeardownResultByDynamicConditionsGoodProduct(@RequestBody Map<String,Object> map){
        // 查询分页数据
        List<urgentTeardownResultPojo> data = urgentTeardownResultService.queryByDynamicConditionsWithPagination(map);
        // 查询符合条件的数据总数
        Integer total = urgentTeardownResultService.queryByDynamicConditionsCount(map);

        // 构建返回结果，保持与现有接口一致的格式
        Map<String, Object> result = new HashMap<>();
        result.put("data", data);
        result.put("total", total);
        return Result.success(result);
    }
}
